package com.upb.webserver.common.constant;

import cn.hutool.core.collection.CollectionUtil;
import com.upb.webserver.common.enums.ChainOrAppStatusEnum;
import com.upb.webserver.common.enums.OrderStatusEnum;
import com.upb.webserver.common.enums.OrderTypeEnum;

import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import static com.upb.webserver.common.enums.ChainOrAppStatusEnum.*;
import static com.upb.webserver.common.enums.RoleTypeEnum.*;

public class Constant {

    // upb web server
//    public static final String WEB_SERVER_APP_ID = "WebServer";
    public static final String DEV_OPS_ADMIN_NAME_ZH = "运维管理员";

    //database
    public static final String COMMON_TABLE_PRIMARY_KEY_NAME = "id";
    public static final String ORDER_BY_GMT_MODIFIED_DESC = "gmt_modified DESC";
    public static final String ORDER_BY_MODIFY_TIME_DESC = "modify_time DESC";

    //http
    public static final int HTTP_SUCCESS_RESPONSE_CODE = 0;

    //workOrder status
    public static final List<Byte> WORK_ORDER_STATUS_HANDLING = Arrays.asList(ALREADY_TO_INITIALIZED.getId(), REMOTE_INITIALIZED.getId(), REMOTE_DEPLOYING.getId(), REMOTE_RESTARTING.getId(), REMOTE_UPGRADING.getId());
    public static final List<Byte> WORK_ORDER_STATUS_DEPLOY_SUCCESS = Arrays.asList(OrderStatusEnum.HANDLE_SUCCESS_BY_MANAGER.getValue(), OrderStatusEnum.PERMIT_BY_MANAGER.getValue());
    public static final List<Byte> SUPPORT_STATUS_BEFORE_DEPLOY = Arrays.asList(OrderStatusEnum.WAIT_FOR_MANAGER_HANDLE.getValue(), OrderStatusEnum.HANDLE_FAIL_BY_MANAGER.getValue());
    public static final List<Byte> SUPPORT_TARGET_STATUS_CLOSE_WORK_ORDER = Arrays.asList(OrderStatusEnum.PERMIT_BY_MANAGER.getValue(), OrderStatusEnum.REJECTED_BY_MANAGER.getValue());
    public static final List<Byte> SUPPORT_DELETE_WORK_ORDER = Arrays.asList(OrderStatusEnum.HANDLE_FAIL_BY_MANAGER.getValue(), OrderStatusEnum.REJECTED_BY_MANAGER.getValue());


    //workOrder type
    public static final List<Byte> ORDER_TYPE_LIST_NEW_CHAIN = Arrays.asList(OrderTypeEnum.NEW_CHAIN_FABRIC.getValue(), OrderTypeEnum.NEW_CHAIN_FISCO.getValue());
    public static final List<Byte> ORDER_TYPE_LIST_NEW_APP = Arrays.asList(OrderTypeEnum.NEW_APPLICATION_FABRIC.getValue(), OrderTypeEnum.NEW_APPLICATION_FISCO.getValue());

    //chain status
    public static final List<Byte> CHAIN_OR_APP_STATUS_DEPLOY_SUCCESS = Arrays.asList(ChainOrAppStatusEnum.RUNNING.getId(), ChainOrAppStatusEnum.UN_NORMAL.getId());
    public static final List<ChainOrAppStatusEnum> CHAIN_STATUS_DEPLOY_FINISH = Arrays.asList(ChainOrAppStatusEnum.DEPLOY_FAIL, ChainOrAppStatusEnum.RUNNING, ChainOrAppStatusEnum.UN_NORMAL);

    //role name
    public static final List<String> ROLE_NAME_LIST_DEVELOPER = Arrays.asList(DEVELOPER_MASTER.getName(), DEVELOPER_GENERAL.getName());
    public static final List<String> ROLE_NAME_LIST_DEV_OPS = Arrays.asList(DEVOPS_ADMIN.getName(), DEVOPS_GENERAL.getName(), API_MANAGER.getName());
    public static final List<String> ROLE_NAME_LIST_AUDIT = Arrays.asList(AUDITOR_ADMIN.getName(), AUDITOR_GENERAL.getName());
    public static final List<String> ROLE_NAME_LIST_API = Arrays.asList(API_MANAGER.getName());
    public static final List<String> ROLE_NAME_LIST_MANAGER = (List<String>) CollectionUtil.union(Constant.ROLE_NAME_LIST_DEV_OPS, Constant.ROLE_NAME_LIST_AUDIT);

    //application
    public static String APPLICATION_FINAL_NAME_FORMAT = "application%1d";

    //fabric org
    public static String FABRIC_ORG_MSP_FORMAT = "%1sMSP";//orgName + "MSP"
    public static String FABRIC_ORG_DOMAIN_FORMAT = "%1s.%2s";//orgName + "." + orgSuffix


    //node type
    public static final String NODE_TYPE_SEALER = "sealer";
    public static final String NODE_TYPE_OBSERVER = "observer";
    public static final String NODE_TYPE_REMOVE = "remove";


    //shell
    public static final Long EXEC_HOST_INIT_TIMEOUT = 60 * 1000L; //mill
    public static final Long EXEC_SHELL_TIMEOUT = 2 * 60 * 1000L;
    public static final String SCP_SHELL = "script/shell/file_trans_util.sh";
    public static final String SHELL_CHECK_HOST = "./script/shell/check_hosts.sh";
    public static final String SHELL_HOST_OPERATE = "./script/shell/host_operate.sh";


    //fabric chainCode:simple file name
    public static final String CHAIN_CODE_SIMPLE_FILE_NAME = "/main.go";


    //fisco
    public static final String PORT_TYPE_RPC = "rpc";
    public static final String PORT_TYPE_P2P = "p2p";
    public static final String PORT_TYPE_CHANNEL = "channel";
    public static final String PORT_TYPE_FRONT = "front";
    public static final String PORT_TYPE_JSONRPCPORT = "jsonrpcPort";
    public static final String PORT_TYPE_P2PPORT = "p2pPort";
    public static final String PORT_TYPE_CHANNELPORT = "channelPort";
    public static final String PORT_TYPE_FRONTPORT = "frontPort";

    public static int PORT_ADD_STEP = 10;  //端口累加值
    public static int FISCO_DEFAULT_JSON_RPC_PORT = 8545;
    public static int FISCO_DEFAULT_P2P_PORT = 30300;
    public static int FISCO_DEFAULT_CHANEL_PORT = 20200;
    public static int FISCO_DEFAULT_FRONT_PORT = 5002;

    //rest return code
    public static final int HYPERLEDGER_INVALID_ID_RETURN_CODE = 207009;


    //p12 file  ./keyFiles/private_key_{timestamp}.p12
    public static String P12_FILE_PARENT_NAME_FORMAT = Paths.get(".", "keyFiles", "%1s").toString();
    public static String P12_FILE_NAME_FORMAT = Paths.get(P12_FILE_PARENT_NAME_FORMAT, "private_key_%2s.p12").toString();
}

